/**
 * 用户改变对活动的状态的Servlet
 */
package org.morningangel.servlet.android;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.morningangel.bean.Status;
import org.morningangel.dao.StatusDao;
import org.morningangel.dao.impl.StatusDaoImpl;

@WebServlet(name = "AndroidActivityStatusServlet", urlPatterns = "/android/activityStatus")
public class ActivityStatusServlet extends HttpServlet {

	private static final long serialVersionUID = 8448640723032893535L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		Integer userId = Integer.parseInt(request.getParameter("userId"));
		String isInterest = request.getParameter("isInterest");
		String isParticipate = request.getParameter("isParticipate");
		Integer activityId = Integer.parseInt(request.getParameter("activityId"));
		StatusDao statusDao = new StatusDaoImpl();
		Status old = statusDao.get(userId, activityId);
		Status status = new Status();
		status.setActivityId(activityId);
		if (isInterest.equals("1") && isParticipate.equals("0")) {
			status.setStatus(1);
		} else if(isParticipate.equals("1") && isInterest.equals("0")) {
			status.setStatus(2);
		} else {
			status.setStatus(3);
		}
		status.setUserId(userId);
		if (old == null) {
			statusDao.save(status);
		} else {
			statusDao.update(old, status);
		}
		PrintWriter out = response.getWriter();
		out.print("{\"isSuccess\":\"1\"}");
		out.flush();
		out.close();
	}

}
